﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;
using DevExpress.Web.ASPxUploadControl;
using DevExpress.Web.ASPxClasses.Internal;

namespace Customer_Profile.eCard
{
    public partial class change_card : System.Web.UI.Page
    {
        const string UploadDirectory = "~/eCard/tempUpload/";
        const int ThumbnailSize = 100;

        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        string settle_idList;

        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            string strURL = HttpContext.Current.Request.Url.AbsolutePath;
            bool bok = Comm.ifhasright(strUserID, strURL);
            if (!bok)
            {
                Response.Write("你无权访问此页面");
                Response.End();
            }
        }

        protected void btnExchange_Click(object sender, EventArgs e)
        {
            if (txtOldCardNo.Text.Trim().Equals("") || txtOldCardNo2.Text.Trim().Equals("") || txtNewCardNo.Text.Trim().Equals("") || txtNewCardNo2.Text.Trim().Equals(""))
            {
                lblWarning.Text = "卡号不能为空";
                return;
            }

            if (txtOldCardNo.Text.Trim()!= txtOldCardNo2.Text.Trim())
            {
                lblWarning.Text = "两次输入的旧卡号不一致";
                return;
            }

            if (txtNewCardNo.Text.Trim() != txtNewCardNo2.Text.Trim())
            {
                lblWarning.Text = "两次输入的新卡号不一致";
                return;
            }

            try
            {
                string attach_url = uploadAttach();                
                string status = "";
                string message = "";                
                DealDao.cardChange(txtOldCardNo.Text.Trim(),txtNewCardNo.Text.Trim(),strUserID,strUserName,attach_url, txtRemark.Text, out status, out message);
                if (status.Equals("Y"))
                {
                    lblWarning.Text = message;                    
                }
                else
                {
                    lblWarning.Text = message;
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = ex.ToString();
            }
        }

        private string uploadAttach()
        {
            string view_name = "";
            string attach_name = "";
            string attach_type = "";
            string attach_size = "";
            string attach_url = "";
            try
            {
                if (FileUpload1.HasFile)
                {
                    FileInfo fileInfo = new FileInfo(FileUpload1.FileName);
                    attach_name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileInfo.Name;
                    string savePathName = MapPath(UploadDirectory) + attach_name;
                    FileUpload1.SaveAs(savePathName);

                    view_name = fileInfo.Name;
                    attach_type = FileUpload1.PostedFile.ContentType.ToString();
                    attach_size = FileUpload1.PostedFile.ContentLength / 1024 + "K";
                    attach_url = "/eCard/tempUpload/" + attach_name;
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = "上传附件失败"+ex.ToString();
            }
            return attach_url;
        }
    }
}